package com.example.a2032.activity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.example.a2032.R;
import com.example.a2032.Utils.SharedPreferenceUtils;
import com.example.a2032.adapter.WatchFaceIndicatorAdapter;
import com.example.a2032.adapter.WatchFaceShowAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WatchFaceActivity extends BaseActivity {

    @BindView(R.id.iv_title)
    ImageView ivTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ViewPager_watchFace)
    ViewPager2 ViewPagerWatchFace;
    @BindView(R.id.RecyclerView_watchFaceIndicator)
    RecyclerView RecyclerViewWatchFaceIndicator;
    private int currentWatchFace;
    int selectedPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watch_face);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        currentWatchFace = SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_WatchFace, 0);
        selectedPosition = currentWatchFace;
        String title = getIntent().getStringExtra(SettingActivity.KEY_SettingTitle);
        tvTitle.setText(title);
        WatchFaceIndicatorAdapter indicatorAdapter = new WatchFaceIndicatorAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        RecyclerViewWatchFaceIndicator.setLayoutManager(linearLayoutManager);
        RecyclerViewWatchFaceIndicator.setAdapter(indicatorAdapter);
        ViewPagerWatchFace.setAdapter(new WatchFaceShowAdapter(this));
        ViewPagerWatchFace.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        ViewPagerWatchFace.setOffscreenPageLimit(3);
        ViewPagerWatchFace.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                selectedPosition = position;
                indicatorAdapter.setSelected(position);
                SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_WatchFace, position);
            }
        });
        ViewPagerWatchFace.setCurrentItem(currentWatchFace);
    }

    @OnClick(R.id.iv_title)
    public void onViewClicked() {

        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // if(selectedPosition!=currentWatchFace)

    }
}